<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">发布</el-button>
      </el-col>
    </el-row>
    <el-table :height="tableHeight" v-loading="loading" :data="list" stripe border>
      <el-table-column label="序号" type="index" width="80" align="center" />
      <el-table-column label="版本" prop="version" width="200" />
      <el-table-column label="路径" prop="url" :show-overflow-tooltip="true" />
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <Z-Dialog :title="title" :visible.sync="open" @confirm="uploadImg" @cancel="cancel">
      <el-form ref="form" :model="form" label-width="100px" :rules="rules">

        <el-form-item label="版本号" prop="version">
          <el-input v-model="form.version" placeholder="请输入版本号如:0.0.0" />
        </el-form-item>

        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="类型选择" clearable class="input-W">
            <el-option v-for="dict in this.typeList" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>

        <el-form-item label="文件上传" prop="version">
          <el-upload action="#" :http-request="requestUpload" :show-file-list="false" :before-upload="beforeUpload">
            <el-button size="small">
              选择
              <i class="el-icon-upload el-icon--right"></i>
            </el-button>
            <i style="margin-left:30px" v-if="this.apkFile.name">已选文件:{{this.apkFile.name}}</i>
          </el-upload>

        </el-form-item>
      </el-form>
    </Z-Dialog>
  </div>
</template>

<script>
import { listAppversion, addAppversion } from "@/api/operation/apk.js";
import { fileUpload } from "@/api/system/user";
import { Loading } from 'element-ui';
export default {
  name: "Appversion",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 信息发布列表数据
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: this.global.pageSize,
      },
      // 表单参数
      form: {},

      apkFile: {},

      typeList: [{ label: 'apk', value: 0 }, { label: 'wgt', value: 1 }],
      rules: {
        version: [{ required: true, message: "版本号必填", trigger: "blur" }],
        url: [{ required: true, message: "必填", trigger: "blur" }],
        type: [{ required: true, message: "类型必须选择", trigger: "blur" }],

      },

    };
  },
  computed: {
    tableHeight() {
      return 'calc(100vh - 230px)'
    },
  },
  created() {
    this.getList();
  },
  methods: {
    // 覆盖默认的上传行为
    requestUpload() {
    },
    getList() {
      this.loading = true;
      listAppversion(this.queryParams)
        .then((response) => {
          if (response.code == 200) {
            this.list = response.data;
            this.total = response.total;
          }
          this.loading = false;
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "提交版本";
    },

    cancel() {
      this.form = {};
      this.apkFile = {};
    },

    // 上传预处理
    beforeUpload(file) {
      // console.log(file);
      // if (file.name.indexOf("apk") == -1) {
      //   this.$modal.msgError("文件格式错误,请上传apk文件。");
      // } else {
      this.apkFile = file;
      // }
    },

    // 上传图片
    uploadImg() {

      this.$refs["form"].validate((valid) => {
        if (valid) {
          let loadingInstance = Loading.service({ text: "正在上传，请稍候", spinner: "el-icon-loading", background: "rgba(0, 0, 0, 0.7)", });

          let formData = new FormData();
          formData.append("file", this.apkFile);
          console.log(this.apkFile);

          // return;

          fileUpload(formData).then(response => {
            this.form.url = response.data.url
            addAppversion(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.$nextTick(() => {
                loadingInstance.close();
              });
            }).catch(err => {
              this.$nextTick(() => {
                loadingInstance.close();
              });
            })
          }).catch(err => {
            this.$nextTick(() => {
              loadingInstance.close();
            });
          });
        }
      });
    }
  },
};
</script>
<style scoped>
</style>
